import 'package:bruno/bruno.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:lazy_food/home/follow/image.dart';
import 'package:lazy_food/home/follow/top.dart';
import 'package:lazy_food/models/follow_model.dart';
import 'package:lazy_food/util/string+.dart';
import '../../components/keepAliveWrapper/keep_alive_arapper.dart';
import '../../models/tab_model.dart';
import '../../net/network.dart';

class FollowPage extends StatefulWidget {
  TabModel tab;
  FollowPage({Key? key, required this.tab}) : super(key: key);

  @override
  FollowPageState createState() => FollowPageState();
}

class FollowPageState extends State<FollowPage> {
  EasyRefreshController _refreshController =
      EasyRefreshController(controlFinishRefresh: true);
  List<FollowModel> _models = [];

  void _onRefresh() {
    EasyLoading.show();
    Network.getHomeFollow().then((value) {
      setState(() {
        _models = value;
      });
      EasyLoading.dismiss();
      _refreshController.finishRefresh();
    });
  }

  List<Widget> _getItem(FollowModel model, double imgW, double imgH) {
    // 用户名和创建时间
    Widget name = Container(
      height: 15,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Expanded(
            child: Text(
              model.user.name,
              style: TextStyle(color: Colors.white, fontSize: 12),
            ),
          ),
          Text(
            model.create_time,
            style: TextStyle(color: Colors.white38, fontSize: 10),
          ),
        ],
      ),
    );
    // 关联菜谱
    Widget recipe = Container(
      height: 15,
      child: Text(
        '做过·${model.recipe_name}',
        maxLines: 1,
        style: TextStyle(color: Colors.white, fontSize: 10),
      ),
    );
    // 笔记内容
    Widget text = Text(
      model.text,
      style: TextStyle(color: Colors.white, fontSize: 14),
      softWrap: true,
    );
    // 图片
    Widget image = ImageView(
      images: model.images,
      width: imgW,
      height: imgH,
    );
    // 功能
    Widget fun = Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        GestureDetector(
          child: Row(
            children: [
              Icon(
                Icons.favorite,
                color: Colors.white54,
                size: 12,
              ),
              SizedBox(width: 2),
              Text(
                '${model.n_diggs}',
                style: TextStyle(color: Colors.white54, fontSize: 12),
              ),
            ],
          ),
          onTap: () {
            EasyLoading.showInfo('点赞');
          },
        ),
        SizedBox(width: 30),
        GestureDetector(
          child: Text(
            '回复',
            style: TextStyle(color: Colors.white54, fontSize: 12),
          ),
          onTap: () {
            EasyLoading.showInfo('回复');
          },
        ),
        SizedBox(width: 30),
        GestureDetector(
          child: Icon(
            Icons.share,
            color: Colors.white54,
            size: 12,
          ),
          onTap: () {
            EasyLoading.showInfo('分享');
          },
        )
      ],
    );
    return model.text.length > 0
        ? [
            name,
            recipe,
            text,
            SizedBox(height: 5),
            image,
            SizedBox(height: 10),
            fun
          ]
        : [name, recipe, SizedBox(height: 5), image, SizedBox(height: 10), fun];
  }

  @override
  Widget build(BuildContext context) {
    return EasyRefresh(
      onRefresh: _onRefresh,
      controller: _refreshController,
      refreshOnStart: true,
      child: KeepAliveWrapper(
        child: ListView.separated(
          itemBuilder: (context, index) {
            if (index == 0) {
              return TopView();
            } else {
              FollowModel model = _models[index - 1];
              double imgW = MediaQuery.of(context).size.width - 110;
              double imgH = imgW * 0.75;
              return Container(
                padding: EdgeInsets.symmetric(horizontal: 20),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    // 头像
                    ClipRRect(
                      borderRadius: BorderRadius.circular(15),
                      child: CachedNetworkImage(
                        imageUrl: model.user.image.url_pattern.toImage(),
                        width: 30,
                        height: 30,
                        placeholder: (context, url) {
                          return Image.asset('assets/images/avatar.png');
                        },
                        fit: BoxFit.cover,
                      ),
                    ),
                    SizedBox(width: 10),
                    Expanded(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: _getItem(model, imgW, imgH),
                      ),
                    )
                  ],
                ),
              );
            }
          },
          separatorBuilder: (context, index) {
            return Divider(
              height: 10,
            );
          },
          itemCount: _models.length + 1,
        ),
      ),
    );
  }
}
